package com.zhiyou.driving.controller;

import com.zhiyou.driving.bean.DayBean;
import com.zhiyou.driving.bean.MonthBean;
import com.zhiyou.driving.model.CourseModel;
import com.zhiyou.driving.service.ICoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * @author by zhouyanchao
 * Created on 2017/10/27.
 * Descr: 教练
 */
@Controller
@RequestMapping("/coach")
public class CoachController {

	@Autowired
	ICoachService coachService;

	@RequestMapping("index")
	public String index(){

		coachService.queryAllCoaches();


		return "index";
	}

	@RequestMapping("setting")
	public String setting(){

		//不封装当月的日历数据

		System.out.println("跳转到教练课程设置页面");
		return "coach_setting";
	}

	/**
	 * Created by zhouyanchao on 2017/10/27
	 * Descr: 只能显示当前月日历的版本
	 *
	 */
	@RequestMapping("calendar")
	public String calendar(Model model){

		//默认返回当前月的数据
		LocalDate localDate = LocalDate.now();
		int curYear = localDate.getYear();
		int curMonth = localDate.getMonthValue();
		int curDay = localDate.getDayOfMonth();
		int curDays = localDate.lengthOfMonth();//本月共多少天
		int firstDayWeek = localDate.withDayOfMonth(1).getDayOfWeek().getValue();//1号是周几

		//当前月已有课程查询
		List<String> courses = getAllCourse(curMonth);

		//封装数据
		List<DayBean> dayBeanList = new ArrayList<>();

		DayBean dayBean = null;
		for(int i=1;i<=curDays;i++){
			dayBean = new DayBean();
			dayBean.setDay(i);
			//是否当天
			if(i==curDay){
				dayBean.setCurrentDay(true);
			}else{
				dayBean.setCurrentDay(false);
			}
			//是否可操作
			if(i<=curDay){
				dayBean.setOperate(false);
			}else{
				dayBean.setOperate(true);
			}
			//是否有课程
			String s = localDate.withDayOfMonth(i).toString();//yyyy-mm-dd
			System.out.println("当前循环："+s);
			if(courses.contains(s)){
				dayBean.setClasses(true);
			}else{
				dayBean.setClasses(false);
			}

			dayBeanList.add(dayBean);
		}

		//返回的数据进行封装
		MonthBean monthBean = new MonthBean();
		monthBean.setYear(curYear);
		monthBean.setMonth(curMonth);
		monthBean.setDays(dayBeanList);
		monthBean.setFirstDayWeek(firstDayWeek);

		model.addAttribute("monthBean",monthBean);

		return "coach_calendar";
	}


	/**
	 * Created by zhouyanchao on 2017/10/27
	 * Descr: 通用版本
	 *
	 */
	@RequestMapping("calendar2")
	public String calendar2(Integer year,Integer month,Integer type, Model model){

		if(year==null){
			year = 0;
		}
		if(month==null || month>12 || month<1){
			month=0;
		}
		//如果type==0，显示给定的年月
		if(type==null){
			type = 0;
		}

		//默认返回当前月的数据
		LocalDate localDate = LocalDate.now();

		//如果年月为0，则默认为当前年月
		if(year==0){
			year = localDate.getYear();
		}
		if(month==0){
			month = localDate.getMonthValue();
		}


		//页面显示需要用到的localDate
		LocalDate displayDate = localDate.withYear(year).withMonth(month);
		if(type<0){
			//上一月
			displayDate = displayDate.minusMonths(1);
		}else if(type>0) {
			//下一月
			displayDate = displayDate.plusMonths(1);
		}

		//根据特定的localDate计算
		int curYear = displayDate.getYear();
		int curMonth = displayDate.getMonthValue();
		int curDays = displayDate.lengthOfMonth();//本月共多少天
		int firstDayWeek = displayDate.withDayOfMonth(1).getDayOfWeek().getValue();//1号是周几

		//当前月已有课程查询
		List<String> courses = getAllCourse(curMonth);

		//封装数据
		List<DayBean> dayBeanList = new ArrayList<>();

		DayBean dayBean = null;
		for(int i=1;i<=curDays;i++){
			dayBean = new DayBean();
			dayBean.setDay(i);
			LocalDate tmpLocalDate = displayDate.withDayOfMonth(i);
			System.out.println("当前循环："+tmpLocalDate);
			dayBean.setDayStr(tmpLocalDate.toString());
			//是否当天
			if(localDate.isEqual(tmpLocalDate)){
				dayBean.setCurrentDay(true);
			}else{
				dayBean.setCurrentDay(false);
			}
			//是否可操作
			if(tmpLocalDate.isBefore(localDate) || tmpLocalDate.isEqual(localDate)){
				dayBean.setOperate(false);
			}else{
				dayBean.setOperate(true);
			}
			//是否有课程
			String s = tmpLocalDate.toString();//yyyy-mm-dd
			if(courses.contains(s)){
				dayBean.setClasses(true);
			}else{
				dayBean.setClasses(false);
			}

			dayBeanList.add(dayBean);
		}

		//返回的数据进行封装
		MonthBean monthBean = new MonthBean();
		monthBean.setYear(curYear);
		monthBean.setMonth(curMonth);
		monthBean.setDays(dayBeanList);
		monthBean.setFirstDayWeek(firstDayWeek);

		model.addAttribute("monthBean",monthBean);

		return "coach_calendar";
	}


	private List<String> getAllCourse(int month){
		List<String> course = new ArrayList<>();
		String m = "";
		if(month<10){
			m = "0"+month;
		}else{
			m = ""+month;
		}
//
//		course.add("2017-"+m+"-25");
//		course.add("2017-"+m+"-27");
//		course.add("2017-"+m+"-30");
//		course.add("2017-"+m+"-05");

		return course;
	}


	/**
	 * Created by zhouyanchao on 2017/10/27
	 * Descr: 根据指定的年月日信息，返回不同状态的内容
	 *
	 * date:要查看的日期信息，格式为 yyyy-mm-dd
	 */
	@RequestMapping("modalinfo")
	public String getModalContnt(Model model,String date){
		System.out.println("日期："+date);

		LocalDate nowDate = LocalDate.now();
		LocalDate disDate = null;//要查看的日期，localDate类型
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date d = dateFormat.parse(date);
			Instant instant = d.toInstant();
			ZoneId zone = ZoneId.systemDefault();
			LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
			disDate = localDateTime.toLocalDate();

		}catch (Exception e){
			e.printStackTrace();
		}

		//是否可操作日期
		boolean operate = false;
		//是否有课程
		boolean course = false;

		if(nowDate.isAfter(disDate) || nowDate.isEqual(disDate)){
			operate = false;
		}else{
			operate = true;
		}

		//教练id为当前登录用户，日期是参数，从数据库查询这一天的课程信息
		//假数据，模拟从数据库查询结果
		List<CourseModel> courseModels = getCourseByCoach(1,date,1);//假设有课程数据
//		List<CourseModel> courseModels = getCourseByCoach(1,date,0);//假设没有课程数据

		//把课程时段整理到map中,key是课程时段，value为是否预约(true未预约，false已预约)
		Map<String,Boolean> courseMap = new HashMap<>();
		if(courseModels!=null && courseModels.size()>0){
			course = true;
			for(CourseModel courseModel : courseModels) {
				courseMap.put(courseModel.getPeriod()+"",courseModel.getStatus()==1?true:false);
			}
		}else{
			course = false;
		}

		//把结果返回到页面处理
		model.addAttribute("operate",operate);
		model.addAttribute("courseMap",courseMap);
		model.addAttribute("date",date);
		model.addAttribute("course",course);

		return "coach_modal_course";
	}

	//返回假数据
	private List<CourseModel> getCourseByCoach(int coachid,String datestr,int type){
		//type : 1是有课程结果信息，-1为没有课程结果信息

		if(type<0){
			return null;
		}

		List<CourseModel> list = new ArrayList<>();
		CourseModel c1 = new CourseModel();
		c1.setId(1);
		c1.setCoachId(coachid);
		c1.setStatus(1);//1未预约
		c1.setPeriod(1);//1上午，2下午，3晚上
//		c1.setCourseDate();
		list.add(c1);

		CourseModel c2 = new CourseModel();
		c2.setId(2);
		c2.setCoachId(coachid);
		c2.setStatus(1);//1未预约,2已预约
		c2.setPeriod(3);//1上午，2下午，3晚上
		list.add(c2);

		CourseModel c3 = new CourseModel();
		c3.setId(2);
		c3.setCoachId(coachid);
		c3.setStatus(2);//1未预约,2已预约
		c3.setPeriod(2);//1上午，2下午，3晚上
		list.add(c3);

		return list;
	}

}
